from pydantic import BaseModel, EmailStr, Field
from typing import Optional, List, Dict, Any
from datetime import datetime


# 用户相关模型
class UserBase(BaseModel):
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: EmailStr = Field(..., description="邮箱")
    is_active: bool = Field(default=True, description="是否激活")
    is_admin: bool = Field(default=False, description="是否为管理员")


class UserCreate(UserBase):
    password: str = Field(..., min_length=6, description="密码")


class UserUpdate(BaseModel):
    username: Optional[str] = Field(None, min_length=3, max_length=50)
    email: Optional[EmailStr] = None
    is_active: Optional[bool] = None
    is_admin: Optional[bool] = None


class UserInDB(UserBase):
    id: str = Field(..., description="用户ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")


class User(UserInDB):
    class Config:
        from_attributes = True


class UserLogin(BaseModel):
    email: EmailStr = Field(..., description="邮箱")
    password: str = Field(..., description="密码")


class Token(BaseModel):
    access_token: str = Field(..., description="访问令牌")
    token_type: str = Field(default="bearer", description="令牌类型")


# AI供应商相关模型
class SupplierBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=50, description="供应商名称")
    api_key: str = Field(..., description="API密钥")
    base_url: Optional[str] = Field(None, description="API基础URL")
    is_active: bool = Field(default=True, description="是否激活")


class SupplierCreate(SupplierBase):
    pass


class SupplierUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=50)
    api_key: Optional[str] = None
    base_url: Optional[str] = None
    is_active: Optional[bool] = None


class Supplier(SupplierBase):
    id: str = Field(..., description="供应商ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")

    class Config:
        from_attributes = True


# AI角色相关模型
class AIRoleBase(BaseModel):
    name: str = Field(..., min_length=1, max_length=100, description="角色名称")
    description: str = Field(..., description="角色描述")
    prompt_template: str = Field(..., description="提示词模板")
    supplier_id: str = Field(..., description="关联的供应商ID")
    is_active: bool = Field(default=True, description="是否激活")
    avatar: Optional[str] = Field(None, description="头像URL")


class AIRoleCreate(AIRoleBase):
    pass


class AIRoleUpdate(BaseModel):
    name: Optional[str] = Field(None, min_length=1, max_length=100)
    description: Optional[str] = None
    prompt_template: Optional[str] = None
    supplier_id: Optional[str] = None
    is_active: Optional[bool] = None
    avatar: Optional[str] = None


class AIRole(AIRoleBase):
    id: str = Field(..., description="角色ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    supplier: Optional[Supplier] = Field(None, description="关联的供应商信息")

    class Config:
        from_attributes = True


# 聊天消息相关模型
class MessageBase(BaseModel):
    content: str = Field(..., description="消息内容")
    role: str = Field(..., description="角色(user/assistant)")
    timestamp: datetime = Field(default_factory=datetime.utcnow, description="消息时间戳")


class MessageCreate(MessageBase):
    pass


class Message(MessageBase):
    id: str = Field(..., description="消息ID")

    class Config:
        from_attributes = True


# 问诊会话相关模型
class ConsultationBase(BaseModel):
    title: Optional[str] = Field(None, max_length=200, description="会话标题")
    user_id: str = Field(..., description="用户ID")
    ai_role_id: str = Field(..., description="AI角色ID")
    status: str = Field(default="active", description="会话状态")
    context: Optional[Dict[str, Any]] = Field(default={}, description="会话上下文")


class ConsultationCreate(ConsultationBase):
    pass


class ConsultationUpdate(BaseModel):
    title: Optional[str] = Field(None, max_length=200)
    status: Optional[str] = None
    context: Optional[Dict[str, Any]] = None


class Consultation(ConsultationBase):
    id: str = Field(..., description="会话ID")
    created_at: datetime = Field(..., description="创建时间")
    updated_at: datetime = Field(..., description="更新时间")
    ai_role: Optional[AIRole] = Field(None, description="AI角色信息")
    last_message: Optional[Message] = Field(None, description="最后一条消息")

    class Config:
        from_attributes = True


# API响应模型
class ResponseBase(BaseModel):
    success: bool = Field(..., description="操作是否成功")
    message: str = Field(..., description="响应消息")


class DataResponse(ResponseBase):
    data: Optional[Any] = Field(None, description="响应数据")


class ListResponse(ResponseBase):
    data: List[Any] = Field(default=[], description="数据列表")
    total: int = Field(0, description="总数据量")
    page: int = Field(1, description="当前页码")
    page_size: int = Field(10, description="每页数据量")


class ErrorResponse(ResponseBase):
    error_code: Optional[str] = Field(None, description="错误代码")
    details: Optional[Any] = Field(None, description="错误详情")